package com.jyy.prefabricated.service.impl;

import com.alibaba.fastjson.JSON;
import com.jyy.prefabricated.dto.AnswerDto;
import com.jyy.prefabricated.entity.Option;
import com.jyy.prefabricated.entity.Question;
import com.jyy.prefabricated.entity.StudentQuizQuestion;
import com.jyy.prefabricated.enums.QuestionTypeEnum;
import com.jyy.prefabricated.mapper.OptionMapper;
import com.jyy.prefabricated.mapper.QuestionMapper;
import com.jyy.prefabricated.mapper.StudentQuizMapper;
import com.jyy.prefabricated.mapper.StudentQuizQuestionMapper;
import com.jyy.prefabricated.service.StudentQuizQuestionService;
import com.jyy.prefabricated.vo.StudentExercisePaperVo;
import com.jyy.prefabricated.vo.StudentExerciseQuestionVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 学生随堂考试题表(StudentQuizQuestion)表服务实现类
 *
 * @author makejava
 * @since 2021-10-11 13:49:17
 */
@Service("studentQuizQuestionService")
public class StudentQuizQuestionServiceImpl implements StudentQuizQuestionService {
    @Resource
    private StudentQuizQuestionMapper studentQuizQuestionMapper;

    @Resource
    private OptionMapper optionMapper;

    @Resource
    private QuestionMapper questionMapper;

    @Resource
    private StudentQuizMapper studentQuizMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public StudentQuizQuestion queryById(Integer id) {
        return this.studentQuizQuestionMapper.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<StudentQuizQuestion> queryAllByLimit(int offset, int limit) {
        return this.studentQuizQuestionMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param studentQuizQuestion 实例对象
     * @return 实例对象
     */
    @Override
    public StudentQuizQuestion insert(StudentQuizQuestion studentQuizQuestion) {
        this.studentQuizQuestionMapper.insert(studentQuizQuestion);
        return studentQuizQuestion;
    }

    /**
     * 修改数据
     *
     * @param studentQuizQuestion 实例对象
     * @return 实例对象
     */
    @Override
    public StudentQuizQuestion update(StudentQuizQuestion studentQuizQuestion) {
        this.studentQuizQuestionMapper.update(studentQuizQuestion);
        return this.queryById(studentQuizQuestion.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.studentQuizQuestionMapper.deleteById(id) > 0;
    }


    /**
     * 随堂考回答答案
     *
     * @param dto
     * @return
     */
    @Override
    public StudentExercisePaperVo answer(AnswerDto dto) {
        StudentQuizQuestion studentQuizQuestion = studentQuizQuestionMapper.queryById(dto.getId());
        if (StringUtils.isNotBlank(dto.getAnswer()) && StringUtils.isBlank(studentQuizQuestion.getAnswer())) {
            //当提交答案不为空并且此题还未作答时时去保存答案
            studentQuizQuestion.setAnswer(dto.getAnswer());
            List<Option> options = optionMapper.selectByQuestionId(studentQuizQuestion.getQuestionId());
            //筛选出正确答案的选项数组
            List<Integer> ids = options.stream().filter(e -> e.getIfAnswer() != null && e.getIfAnswer()).map(e -> e.getId()).collect(Collectors.toList());
            List<Integer> submitAnswers = JSON.parseArray(dto.getAnswer(), Integer.class);
            Boolean ifRight = true;
            for (Integer optionId : ids) {
                if (!submitAnswers.contains(optionId) || submitAnswers.size() != ids.size()) {
                    ifRight = false;
                }
            }

            Question qe = questionMapper.selectByPrimaryKey(studentQuizQuestion.getQuestionId());
            if (qe.getQuestionType().equals(QuestionTypeEnum.JUDGE.getValue())) {
                if (qe.getJudgmentAnswer() && submitAnswers.contains(1)) {
                    ifRight = true;

                } else if (qe.getJudgmentAnswer() == false && submitAnswers.contains(2)) {
                    ifRight = true;

                } else {
                    ifRight = false;
                }

            }

            studentQuizQuestion.setIfRight(ifRight);
            update(studentQuizQuestion);
        }
        return fetchQuestionsByStudentQuizId(studentQuizQuestion.getStudentQuizId());
    }

    /**
     * 根据学员随堂考id获取学员练习题卷
     *
     * @param studentQuizId
     * @return
     */
    @Override
    public StudentExercisePaperVo fetchQuestionsByStudentQuizId(Integer studentQuizId) {
        StudentExercisePaperVo paperVo = studentQuizMapper.selectByStudentQuizId(studentQuizId);
        List<StudentExerciseQuestionVo> questions = studentQuizQuestionMapper.selectQuestionVoByStudentQuizId(studentQuizId);
        paperVo.setQuestionCount(questions.size());
        paperVo.setQuestions(questions);
        return paperVo;
    }

}
